//
//  HMWelcomeViewController.swift
//  HMWeibo1
//
//  Created by liGordon on 16/4/11.
//  Copyright © 2016年 lifuli. All rights reserved.
//

import UIKit

import SnapKit
import SDWebImage

// swift 定义枚举 , 使用原始值 0
enum season: Int {
    case Spring = 0
    case Summer
    case Autumn
    case Winter
}

class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        
        
         //之前有没有用户信息
        setIcon()
        
        
        // 发送网络请求. 获取用户信息
        HMUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                HMPrint("获取用户信息出错了: \(error)")
                return
            }
            
            // 获取用户信息成功了
            self.setIcon()
        }
        
    }
    
    private func setIcon() {
        
        if let avatar_large = HMUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            
            // 网络请求完了在调用这段代码
            // 使用SD来下载图片
            let url = NSURL(string: avatar_large)!
           self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            
        }
        
    }
    
    private func prepareUI() {
        
        view.addSubview(bgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        bgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
            
            
            
            //  尺寸约束...............
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
   
        
        // label
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
     
      }
          // 在view看得见的时候做动画
        override func viewDidAppear(animated: Bool) {
            super.viewDidAppear(animated)
            iconViewAnimation()
        }
        
        /// 头像动画
        private func iconViewAnimation() {
        // 修改头像底部约束, 动画
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            }
            
            UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                // 通知系统来重新设置布局
                self.view.layoutIfNeeded()
                }) { (_) -> Void in
                    // 让label显示出来
                    UIView.animateWithDuration(HMDefaultAnimDuration, animations: { () -> Void in
                        self.welcomeLabel.alpha = 1
                        }, completion: { (_) -> Void in
                            HMPrint("动画完成了要切换界面")
                            
                            // 切换到mainViewController
                            let controller = HMMainViewController()
                            (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(controller)
                            
                    })
            }

    }
    

    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
//        // 圆角
        imageView.layer.cornerRadius = 42.5

        // 裁剪
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    private lazy var welcomeLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "每天吃榨菜酱香饼真是够了"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(HMWelcomeLabelFontSize)
        label.sizeToFit()
        
        return label
        
    }()
    
}
